using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration.Install;
using System.Collections.Generic;
using System.Diagnostics;

namespace ODSControllerService
{
	/// <summary>
	/// Summary description for ProjectInstaller.
	/// </summary>
	[RunInstaller(true)]
	public class ProjectInstaller : System.Configuration.Install.Installer
	{
      private System.ServiceProcess.ServiceProcessInstaller serviceProcessInstaller1;
		private System.ServiceProcess.ServiceInstaller serviceInstaller1;
		/// <summary>
		/// Required designer variable.
		/// </summary>

		public ProjectInstaller()
		{
			// This call is required by the Designer.
			InitializeComponent();

			// TODO: Add any initialization after the InitComponent call
			this.serviceProcessInstaller1.Account=System.ServiceProcess.ServiceAccount.LocalSystem;
			this.serviceInstaller1.StartType=System.ServiceProcess.ServiceStartMode.Automatic;
			this.serviceInstaller1.DisplayName="ODS Controller Service";
		}

		#region Component Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.serviceProcessInstaller1 = new System.ServiceProcess.ServiceProcessInstaller();
         this.serviceInstaller1 = new System.ServiceProcess.ServiceInstaller();
         // 
         // serviceProcessInstaller1
         // 
         this.serviceProcessInstaller1.Password = null;
         this.serviceProcessInstaller1.Username = null;
         // 
         // serviceInstaller1
         // 
         this.serviceInstaller1.DisplayName = "ODS Contoller service";
         this.serviceInstaller1.ServiceName = "ODS_Service";
         this.serviceInstaller1.BeforeInstall += new System.Configuration.Install.InstallEventHandler(this.serviceInstaller1_BeforeInstall);
         // 
         // ProjectInstaller
         // 
         this.Installers.AddRange(new System.Configuration.Install.Installer[] {
            this.serviceProcessInstaller1,
            this.serviceInstaller1});

		}
		#endregion


		public override void Install(IDictionary stateServer) {
			Microsoft.Win32.RegistryKey system, software, currentControlSet, services, service, config;

			try {
				base.Install(stateServer);
				//Open the HKEY_LOCAL_MACHINE\SYSTEM key to add the service description
				system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
				currentControlSet = system.OpenSubKey("CurrentControlSet");
				services = currentControlSet.OpenSubKey("Services");
				service = services.OpenSubKey(this.serviceInstaller1.ServiceName, true);
				service.SetValue("Description", "Check and update ODS records in the background.");
            service.Close();
            currentControlSet.Close();
            system.Close();

            //Debugger.Launch();
				
            //add the install type
            if (null != Context.Parameters["PRODUCTNAME"]) {
               software = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE");
               config = software.OpenSubKey(Context.Parameters["PRODUCTNAME"],true);
               config.SetValue("ServiceInstalled", 1, Microsoft.Win32.RegistryValueKind.DWord);
               config.Close();
            }
			}
			catch(Exception e) {
				Console.WriteLine("An exception was thrown during service installation:\n" + e.ToString());
			}
		}

		public override void Uninstall(IDictionary stateServer) {
         Microsoft.Win32.RegistryKey system, software, currentControlSet, services, service, config;

			try{
				system = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("System");
				currentControlSet = system.OpenSubKey("CurrentControlSet");
				services = currentControlSet.OpenSubKey("Services");
				service = services.OpenSubKey(this.serviceInstaller1.ServiceName, false);
            service.Close();
            currentControlSet.Close();
            system.Close();

            //add the install type
            if (null != Context.Parameters["PRODUCTNAME"]) {
               software = Microsoft.Win32.Registry.LocalMachine.OpenSubKey("SOFTWARE");
               config = software.OpenSubKey(Context.Parameters["PRODUCTNAME"],true);
               config.SetValue("ServiceInstalled", 0, Microsoft.Win32.RegistryValueKind.DWord);
            }

				//if(val=="1"){
					base.Uninstall(stateServer);
				//}
				//service.DeleteSubKeyTree("Parameters");
			}
			catch(Exception e){
				Console.WriteLine("An exception was thrown during uninstall:\n" + e.ToString());
			}
		}

      private void serviceInstaller1_BeforeInstall(object sender, InstallEventArgs e) {
         
      }

	}
}
